import 'package:flutter/material.dart';
import 'page_router_sub1.dart';
import 'page_router_sub2.dart';

class PageRouter extends StatefulWidget {
  PageRouter({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _PageRouterState createState() => _PageRouterState();
}

class _PageRouterState extends State<PageRouter> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void navigate(){
    //导航到新路由
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return PageRouteSub1();
    }));
  }

  void navigateWithParams() async{
    // 打开`TipRoute`，并等待返回结果
    var result = await Navigator.push(
      context,
      MaterialPageRoute(  
        builder: (context) {
          return PageRouteSub2(
            // 路由参数
            text: "我是提示xxxx",
          );
        },
      ),
    );
    //输出`TipRoute`路由返回结果
    print("路由返回值: $result");
  }

  void navigateNamed(){
    Navigator.pushNamed(context, "page_router_sub1");
  }

  void navigateNamedWithParams() async{
    var result = await Navigator.of(context).pushNamed("page_router_sub2", arguments: "hi");
    //输出`TipRoute`路由返回结果
    print("路由返回值: $result");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times水水水水:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            FlatButton(
              child: Text("打开新页面"),
              textColor: Colors.blue,
              onPressed: navigate),
            FlatButton(
                child: Text("打开新页面并传参"),
                textColor: Colors.amber,
                onPressed: navigateWithParams),
            FlatButton(
                child: Text("使用命名路由打开新页面"),
                textColor: Colors.brown,
                onPressed: navigateNamed),
            FlatButton(
                child: Text("使用命名路由打开新页面并传参"),
                textColor: Colors.orange,
                onPressed: navigateNamedWithParams)
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
